package cn.chinasyq.photoquan.user.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import java.util.List;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.user.bean.GameOrder;
import cn.master.volley.commons.VolleyHelper;

/**
 * Created by 一搏 on 2015/12/17.
 */
public class GameOrderListAdapter extends BaseAdapter {
    private ArrayList<GameOrder> orders;

    public GameOrderListAdapter() {
        orders = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return orders.size();
    }

    @Override
    public Object getItem(int position) {
        return orders.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        GameOrder item = (GameOrder) getItem(position);
        if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof ViewHolder)) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_game_order, null);
            viewHolder.iv_1 = (NetworkImageView) convertView.findViewById(R.id.iv);
            viewHolder.tv_create_time = (TextView) convertView.findViewById(R.id.tv_create_time);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_status = (TextView) convertView.findViewById(R.id.tv_status);
            viewHolder.btn1 = (TextView) convertView.findViewById(R.id.button1);
            viewHolder.btn2 = (TextView) convertView.findViewById(R.id.button2);
            viewHolder.btn1.setOnClickListener((View.OnClickListener) parent.getContext());
            viewHolder.btn2.setOnClickListener((View.OnClickListener) parent.getContext());
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_create_time.setText(item.getCreated_at());
        viewHolder.tv_title.setText(item.getTitle());
        viewHolder.tv_status.setText(item.obtainStatic());
        viewHolder.iv_1.setImageUrl(item.getImage(), VolleyHelper.getImageLoader());
        viewHolder.btn1.setTag(item);
        viewHolder.btn2.setTag(item);
        if ("main_create".equals(item.getState().getVal())){
            viewHolder.btn1.setText("待付款");
            viewHolder.btn1.setBackgroundResource(R.drawable.btn_c8_c10);
            viewHolder.btn1.setTextColor(parent.getResources().getColor(R.color.C8));
            viewHolder.btn2.setVisibility(View.VISIBLE);
            viewHolder.btn1.setVisibility(View.VISIBLE);
        }else {
            viewHolder.btn1.setVisibility(View.GONE);
            viewHolder.btn2.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder {
        TextView tv_create_time, tv_title, tv_status;
        NetworkImageView iv_1;
        TextView btn1, btn2;
    }

    public void setOrders(ArrayList<GameOrder> orders) {
        this.orders = orders;
        notifyDataSetChanged();
    }

    public void addOrders(List<GameOrder> albums) {
        this.orders.addAll(albums);
        notifyDataSetChanged();
    }
}
